<template>
    <div>
        <el-card>
            <div id="main"></div>
        </el-card>
    </div>
</template>
<script>
import * as echarts from "echarts";
import service from '@/request';
export default {
    
  data() {
    return {
      name: "各门店商品量",
      xdata: [], //横坐标数据
      ydata: [] //纵坐标数据，与横坐标对应
    };
  },
  mounted() {

    this.initEcharts();
  },
  methods: {
  async  initEcharts() {
        const res=await service.post("/chart/chart1");
        console.log(res);
        res.data.forEach(el=>{
            this.xdata.push(el.name)
            this.ydata.push(el.count)
        })

        const option = {
        title: {
          text: "各门店商品数量"
        },
        tooltip: {},
        legend: {
          data: ["销量"]
        },
        xAxis: {
          data: this.xdata
        },
        yAxis: {
         name:"123"
        },
        series: [
          {
            name: "销量",
            type: "bar", //类型为柱状图
            data: this.ydata
          }
        ]
      };
      const myChart = echarts.init(document.getElementById("main"));// 图标初始化
      myChart.setOption(option);// 渲染页面
      //随着屏幕大小调节图表
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    }
  }
};
</script>

<style>
#main{
    width:400px;
    height:400px;
}
</style>